package com.huawei.dms.rocketmq.lifecycle;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.rocketmq.v2.RocketMQClient;
import com.huaweicloud.sdk.rocketmq.v2.model.CreatePostPaidInstanceReq;
import com.huaweicloud.sdk.rocketmq.v2.model.CreatePostPaidInstanceRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.CreatePostPaidInstanceResponse;

import java.util.ArrayList;
import java.util.List;

import com.huaweicloud.sdk.rocketmq.v2.model.DeleteInstanceRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.DeleteInstanceResponse;
import com.huaweicloud.sdk.rocketmq.v2.model.ListAvailableZonesRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.ListAvailableZonesResponse;
import com.huaweicloud.sdk.rocketmq.v2.model.ShowInstanceRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.ShowInstanceResponse;
import com.huaweicloud.sdk.rocketmq.v2.model.UpdateInstanceReq;
import com.huaweicloud.sdk.rocketmq.v2.model.UpdateInstanceRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.UpdateInstanceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RocketMQInstanceLifeCycleDemo {
    private static final Logger logger = LoggerFactory.getLogger(RocketMQInstanceLifeCycleDemo.class);

    public static void main(String[] args) {
        String ak = "<YOUR ak>";
        String sk = "<YOUR sk>";
        String endpoint = "<YOUR Endpoint String>";
        String projectId = "<YOUR Project Id>";
        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
        RocketMQClient client = RocketMQClient.newBuilder()
                .withCredential(auth)
                .withEndpoint(endpoint)
                .build();
        // 查询可用区
        listAvailableZones(client);
        // 创建实例
        createPostPaidInstance(client);
        // 修改实例
        updateInstance(client);
        // 查询实例
        showInstance(client);
        // 删除实例
        deleteInstance(client);
    }

    private static void createPostPaidInstance(RocketMQClient client) {
        try {
            CreatePostPaidInstanceRequest request = new CreatePostPaidInstanceRequest();
            CreatePostPaidInstanceReq body = new CreatePostPaidInstanceReq();
            /* 必要 */
            body.withName("<YOUR InstanceName>");
            body.withEngine(CreatePostPaidInstanceReq.EngineEnum.fromValue("reliability"));
            body.withEngineVersion(CreatePostPaidInstanceReq.EngineVersionEnum.fromValue("4.8.0"));
            body.withStorageSpace(600);
            // vpc、安全组、子网
            body.withVpcId("<YOUR VpcId>");
            body.withSubnetId("<YOUR SubnetId>");
            body.withSecurityGroupId("<YOUR SecurityGroupId>");
            // 可用区的id，通过ListAvailableZones获得
            List<String> listbodyAvailableZones = new ArrayList<>();
            listbodyAvailableZones.add("<AvailableZones>");
            body.withAvailableZones(listbodyAvailableZones);
            // 产品标识的id
            // c6.4u8g.cluster.small: 单个代理最大Topic数2000，单个代理最大消费组数2000
            // c6.4u8g.cluster：单个代理最大Topic数4000，单个代理最大消费组数4000
            // c6.8u16g.cluster：单个代理最大Topic数8000，单个代理最大消费组数8000
            // c6.12u24g.cluster：单个代理最大Topic数12000，单个代理最大消费组数12000
            // c6.16u32g.cluster：单个代理最大Topic数16000，单个代理最大消费组数16000
            body.withProductId(CreatePostPaidInstanceReq.ProductIdEnum.fromValue("c6.4u8g.cluster"));
            // dms.physical.storage.high.v2: 高IO类型磁盘
            // dms.physical.storage.ultra.v2: 超高IO类型磁盘
            body.withStorageSpecCode(CreatePostPaidInstanceReq.StorageSpecCodeEnum.fromValue("dms.physical.storage.high.v2"));
            /* 非必要 */
            body.setDescription("Description Info");
            body.withSslEnable(false);
            body.withIpv6Enable(false);
            body.setEnablePublicip(false);
            // 实例绑定的弹性IP地址的ID,enablePublicip为true必填
            body.setPublicipId("<YOUR Specifies the ID of the elastic IP address>");
            body.withBrokerNum(1);
            request.withBody(body);
            CreatePostPaidInstanceResponse response = client.createPostPaidInstance(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static void showInstance(RocketMQClient client) {
        try {
            ShowInstanceRequest request = new ShowInstanceRequest();
            request.withInstanceId("<YOUR InstanceId>");
            ShowInstanceResponse response = client.showInstance(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static void deleteInstance(RocketMQClient client) {
        try {
            DeleteInstanceRequest request = new DeleteInstanceRequest();
            request.withInstanceId("<YOUR InstanceId>");
            DeleteInstanceResponse response = client.deleteInstance(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static void updateInstance(RocketMQClient client) {
        try {
            UpdateInstanceRequest request = new UpdateInstanceRequest();
            request.withInstanceId("<YOUR InstanceId>");
            UpdateInstanceReq body = new UpdateInstanceReq();
            body.withName("<YOUR InstanceName>");
            body.withDescription("Description");
            body.withSecurityGroupId("<YOUR SecurityGroupId>");
            // ACL访问控制。
            body.withRetentionPolicy(false);
            request.withBody(body);
            UpdateInstanceResponse response = client.updateInstance(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static void listAvailableZones(RocketMQClient client) {
        try {
            ListAvailableZonesRequest request = new ListAvailableZonesRequest();
            ListAvailableZonesResponse response = client.listAvailableZones(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}